package com.icart.controller.custcardController;

import com.icart.context.HttpUserContext;
import com.icart.context.storage.ContextStorage;
import com.icart.context.storage.SessionContextStorageImpl;
import com.icart.repositories.CucustmemcardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("/custcard")
public class CustcardController {
    @Autowired
    HttpServletRequest request;
    @Autowired
    CucustmemcardRepository cucustmemcardRepository;

    /*我的会员卡首页*/
    @RequestMapping("/custcardHome")
    public String custcardHome(Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*先查出计次卡*/
        List list=cucustmemcardRepository.createNativeQuery("select d.cardid,d.cardno,DATE_FORMAT(d.expidate,'%Y-%m-%d')expidate," +
                "(select n.chnname from bi_chn n where n.chnid=d.creatchn)chnname,(select k.tcname from mk_discpack k where k.tcid=d.tcid)tcname " +
                "from cu_custmemcard d where d.cardtype='mouldtc' and d.custid="+userContext.getCustid());
        model.addAttribute("list",list);

        /*查出储值卡*/
        List list2=cucustmemcardRepository.createNativeQuery("select d.cardid,d.cardno,DATE_FORMAT(d.expidate,'%Y-%m-%d')expidate," +
                "(select n.chnname from bi_chn n where n.chnid=d.creatchn)chnname,(select k.tcname from mk_discpack k where k.tcid=d.tcid)tcname " +
                "from cu_custmemcard d where d.cardtype='mouldprepay' and d.custid="+userContext.getCustid());
        model.addAttribute("list2",list2);
        return "custcard/custcardhome";
    }


    /*套餐计次会员卡详情*/
    @RequestMapping("/custcarddetail")
    public String custcarddetail(@RequestParam(name = "cardid",required = false) String cardid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*重复展示卡的内容*/
        List list=cucustmemcardRepository.createNativeQuery("select d.cardid,d.cardno,DATE_FORMAT(d.expidate,'%Y-%m-%d')expidate,d.remark," +
                "(select n.chnname from bi_chn n where n.chnid=d.creatchn)chnname,(select k.tcname from mk_discpack k where k.tcid=d.tcid)tcname " +
                "from cu_custmemcard d where d.cardtype='mouldtc' and d.custid="+userContext.getCustid()+" and d.cardid="+cardid);
        model.addAttribute("mouldtc",list.get(0));

        /*展示卡的套餐*/
        List tc=cucustmemcardRepository.createNativeQuery("select (select k.tcname from mk_discpack k where k.tcid=c.tcid)tcname,c.tcount from cu_custmemcardintc c " +
                "where c.cardid="+cardid+" and c.custid="+userContext.getCustid());
        model.addAttribute("tc",tc);

        /*展示卡的服务项目*/
        List servitem=cucustmemcardRepository.createNativeQuery("select (select n.servitemname from bi_servitem n where n.servitemid=m.servitem)servitemname,m.tcount from cu_custmemcardinservitem m " +
                " where m.cardid="+cardid+" and m.custid="+userContext.getCustid());
        model.addAttribute("servitem",servitem);
        return "custcard/custcarddetail";
    }

    /*套餐计次会员卡消费历史*/
    @RequestMapping("/payhistory")
    public String payhistory(@RequestParam(name = "cardid",required = false) String cardid,Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*展示其他历史，开卡和退卡*/
        List list=cucustmemcardRepository.createNativeQuery("select d.saleamt,(select n.chnname from bi_chn n where n.chnid=d.creatchn)chnname," +
                "g.chgtype,g.chgval,DATE_FORMAT(g.creattime,'%Y-%m-%d')creattime" +
                " from cu_custmemcard d,cu_custmemcardlog g " +
                "where d.cardid=g.cardid and g.chgtype in('opencard','closecard') and d.cardid="+cardid+" and d.custid="+userContext.getCustid() +" group by g.chgtype");
        model.addAttribute("other",list);

        /*展示消费历史*/
        List list2=cucustmemcardRepository.createNativeQuery("select (select n.chnname from bi_chn n where n.chnid=v.chnid)chnname,v.carno," +
                "DATE_FORMAT(g.creattime,'%Y-%m-%d')creattime,(select m.servitemname from bi_servitem m where m.servitemid=v.servitemid)servitemname " +
                "from cu_custmemcardlog g,wo_infoserv v where g.chgtype='used' and g.relateoid=v.oid and g.custid=v.custid and g.cardid="+cardid+" and g.custid="+userContext.getCustid());
        model.addAttribute("pay",list2);
        return "custcard/payhistory";
    }

    /*储值卡详情*/
    @RequestMapping("/mouldprepaydetail")
    public String mouldprepaydetail(@RequestParam(name = "cardid",required = false) String cardid, Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

         /*重复展示卡的内容*/
        List list=cucustmemcardRepository.createNativeQuery("select d.cardid,d.cardno,DATE_FORMAT(d.expidate,'%Y-%m-%d')expidate,d.remark," +
                "(select n.chnname from bi_chn n where n.chnid=d.creatchn)chnname,(select k.tcname from mk_discpack k where k.tcid=d.tcid)tcname," +
                "k.totalval,k.saleprice,k.presentval,d.balance " +
                "from cu_custmemcard d,mk_discpack k where d.tcid=k.tcid and d.custid="+userContext.getCustid()+" and d.cardid="+cardid);
        model.addAttribute("mouldprepay",list.get(0));

        return "custcard/mouldprepaydetail";
    }

    /*储值卡消费历史*/
    @RequestMapping("/mouldprepayhistory")
    public String mouldprepayhistory(@RequestParam(name = "cardid",required = false) String cardid,Model model) {
        ContextStorage storage=new SessionContextStorageImpl(request);
        HttpUserContext userContext=storage.getUserContext("loginuser");

        /*展示其他历史，开卡和退卡*/
        List list=cucustmemcardRepository.createNativeQuery("select (select n.chnname from bi_chn n where n.chnid=d.creatchn)chnname," +
                "g.chgtype,g.afterchgval,g.chgval,DATE_FORMAT(g.creattime,'%Y-%m-%d')creattime" +
                " from cu_custmemcard d,cu_custmemcardlog g " +
                "where d.cardid=g.cardid and g.chgtype in('opencard','closecard') and d.cardid="+cardid+" and d.custid="+userContext.getCustid());
        model.addAttribute("other",list);

        /*展示消费历史*/
        List list2=cucustmemcardRepository.createNativeQuery("select (select n.chnname from bi_chn n where n.chnid=g.creatchn)chnname," +
                "(select o.carno from wo_info o where o.oid=g.relateoid)carno," +
                "DATE_FORMAT(g.creattime,'%Y-%m-%d')creattime,g.chgval " +
                "from cu_custmemcardlog g where g.chgtype='used' and g.cardid="+cardid+" and g.custid="+userContext.getCustid());
        model.addAttribute("pay",list2);
        return "custcard/mouldprepayhistory";
    }
}